package org.social.controller;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import com.sojoys.artifact.third.web.core.NettyHttpContext;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.util.ClassLoaderUtil;

/**
 * @author : DengYing
 * @CreateDate : 2018年2月7日 下午4:11:13
 * @Description ：热修复
 */
public class HotUpdateServer {
	public void exec(NettyHttpContext ctx) {
		try {
			// 补丁文件路径
			String patchFilePath = ctx.params().getString("patchFilePath");
			// 执行类名称
			String className = ctx.params().getString("className");
			File patchFile = FileUtil.file(patchFilePath);
			URLClassLoader loader = new URLClassLoader(new URL[] { patchFile.toURI().toURL() },ClassLoaderUtil.getClassLoader());

			Class<?> clazz = loader.loadClass(className);

			Method m = clazz.getMethod("execute", null);
			
			m.invoke(clazz.newInstance(), null);
			
			FileUtil.del(patchFile);
			ctx.send("执行热更新成功！");
		} catch (Exception e) {
			e.printStackTrace();
			ctx.send("执行热更新失败！");
		}
	}
}
